<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of FileController
 *
 * @author Lisette
 */
class FileController extends BaseController {
    
    
    
    public function save($path, $rand, $url){
    	
    	//$this->check();
    	if (is_uploaded_file($_FILES['form_new_file']['tmp_name'])) {
    		
    		$pdf_contents = file_get_contents($_FILES['form_new_file']['tmp_name'], NULL, NULL, 0, 100);
    		if(stripos($pdf_contents, '%PDF')===0) {
    			move_uploaded_file($_FILES['form_new_file']['tmp_name'], $path.DIRECTORY_SEPARATOR.$rand.".pdf");
    			$return = true;
    		} else {
    			$error[] = "Error! This file is not a PDF.";
    			$return = false;
    		}
    	}
    	if (!empty($_POST['form_new_file_link']) || isset($url)) {
    		
    		if(isset($url)){
    			$filename = $url;
    		}else
    			$filename = $_POST['form_new_file_link'];
    		
    		
    		$pdf_contents = proxy_file_get_contents($filename, $proxy_name, $proxy_port, $proxy_username, $proxy_password);
    		if(stripos($pdf_contents, '%PDF')===0) {
    			if (!empty($pdf_contents)) {
    				file_put_contents($path.DIRECTORY_SEPARATOR.$rand.".pdf",$pdf_contents);
    				$return = true;	
    			}
    		} else {
    			$error[] = "Error! This file is not a PDF.";
    			$return = false;
    		}
    	}
    	
    	
    	$temp_dir = sys_get_temp_dir();
		if (substr($temp_dir, -1) == DIRECTORY_SEPARATOR) $temp_dir = substr($temp_dir, 0, -1);
		
		
    	if (isset($_POST['filename']) && is_readable($temp_dir.DIRECTORY_SEPARATOR.$_POST['filename'])) {
    		
    		$copy = copy ($temp_dir.DIRECTORY_SEPARATOR.$_POST['filename'], $path.DIRECTORY_SEPARATOR.$rand.'.pdf');
    		unlink ($temp_dir.DIRECTORY_SEPARATOR.$_POST['filename']);
    		if ($copy == false){
    			$return = false;
    		}
    		if ($copy == true) {
    			$return = true;
    		}
    		
    	}
    	
    	if($return){
    		$return_message = array($return, $message);
    	}
    	else{
    		$return_message = array($return, $error);
    	}
    	 
    	return $return_message; //true success upload!
    }
    
    /**
     *
     * @param type $document
     * @param type $action
     * @param type $temp_dir
     * @return boolean 
     */
   protected function check(){
	   	$directives = '<BR><BR><U>Relevant directives in php.ini:</U>';
	   	$directives .= '<BR>file_uploads: '.ini_get ('file_uploads').' (required 1)';
	   	$directives .= '<BR>upload_max_filesize: '.ini_get ('upload_max_filesize').' (recommended 60M)';
	   	$directives .= '<BR>post_max_size: '.ini_get ('post_max_size').' (recommended 60M)';
	   	$directives .= '<BR>max_input_time: '.ini_get ('max_input_time').' (recommended 60)';
	   	
	   	if (isset($_FILES['form_new_file']['error']) && $_FILES['form_new_file']['error'] > 0) {
	   	
	   		if ($_FILES['form_new_file']['error'] == 1) die("The uploaded file size exceeds the <b>upload_max_filesize</b> directive in <b>php.ini</b>.$directives");
	   		if ($_FILES['form_new_file']['error'] == 3) die("The uploaded file was only partially uploaded.$directives");
	   		if ($_FILES['form_new_file']['error'] == 6) die("Missing a temporary folder.$directives");
	   		if ($_FILES['form_new_file']['error'] == 7) die("Failed to write file to disk.$directives");
	   		if ($_FILES['form_new_file']['error'] == 8) die("File upload stopped by extension.$directives");
	   	}
   }
    

    /**
     * local or url
     * @param type $proxy
     * @return null|\File 
     */
    private function bind($proxy) {
        $pdf_contents = null;

        if (isset($_FILES['form_new_file']) && is_uploaded_file($_FILES['form_new_file']['tmp_name']))
            $pdf_contents = file_get_contents($_FILES['form_new_file']['tmp_name'], NULL, NULL, 0, 100);
        if (!empty($_POST['form_new_file_link']))
            $pdf_contents = proxy_file_get_contents($_POST['form_new_file_link'], $proxy['name'], $proxy['port'], $proxy['username'], $proxy['password']);

        if (!isset($pdf_contents))
            return null;

        $file = new File();
        $file->setFilename($_FILES['form_new_file']['name']);
        $file->setUrl(empty($_POST['form_new_file_link']) ? null : $_POST['form_new_file_link']);
        $file->setSize($_FILES['form_new_file']["size"]);
        $file->setType($_FILES['form_new_file']["type"]);
        $file->setContent($pdf_contents);
        $file->setTmpName($_FILES['form_new_file']["tmp_name"]);

        return $file;
    }

}

?>
